﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using log4net;
using Mate.Caching;
using Mate.Core.Base;
using Mate.Db.Model.Merchant;
using Mate.Framework.BaseController.Admin;
using Mate.Merchant.Service;
using Microsoft.AspNetCore.Mvc;

namespace Mate.Admin.Controllers.Merchant
{
    public class MerchantOpentimeController : BaseController
    {
        private readonly IMerchantService merchantService;
        private readonly IMerchantOpentimeService merchantOpentimeService;
        private readonly ILog log;
        private readonly ICacheManager _cache;
        public MerchantOpentimeController(IMerchantService merchantService, IMerchantOpentimeService merchantOpentimeService, ICacheManager cache, ILog log)
        {
            this.log = log;
            this.merchantService = merchantService;
            this.merchantOpentimeService = merchantOpentimeService;
            this._cache = cache;
        }

        public IActionResult Index(int merchantId)
        {
            ViewBag.MerchantId = merchantId;
            return View();
        }

        #region 获取列表
        [HttpPost]
        public IActionResult GetList(int pageIndex, int pageSize, int merchantId)
        {
            int recordCount = 0;
            var list = merchantOpentimeService.Queryable.Where(x=>x.is_delete==1&&x.merchantid==merchantId)
               .OrderBy(x => x.id, SqlSugar.OrderByType.Desc)
               .ToPageList(pageIndex, pageSize, ref recordCount);
            var result = new
            {
                rows = list,
                total = recordCount
            };
            return Json(result);
        }
        #endregion

        #region 删除
        [HttpPost]
        public JsonResult Remove(int id)
        {
            var result = new BaseResult();

            var model = merchantOpentimeService.Get(x => x.id == id);
            if (model == null)
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "参数错误";
                return Json(result);
            }

            var b = merchantOpentimeService.Update(new mp_merchant_opentime()
            {
                id = id,
                is_delete = 2
            }, x => new object[] { x.is_delete }, x => new object[] { x.id });
            if (b > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "删除成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "删除失败";
            }
            return Json(result);
        }
        #endregion

        #region 添加
        public ActionResult Create(int merchantId)
        {
            var model = new mp_merchant_opentime() { is_open = 1 ,merchantid=merchantId};
            ViewBag.Opentime = OpenTimeList();
            return View(model);
        }

        [HttpPost]
        public JsonResult Create(mp_merchant_opentime model)
        {
            var result = new BaseResult();
            var sTime = Convert.ToDateTime(DateTime.Now.Date.ToString("yyyy-MM-dd") + " " + model.merchantopenbegin);
            var eTime = Convert.ToDateTime(DateTime.Now.Date.ToString("yyyy-MM-dd") + " " + model.merchantopenend);
            if (sTime >= eTime)
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "结束时间必须大于开始时间";
                return Json(result);
            }
            model.is_delete = 1;
            var obj = merchantOpentimeService.Insert(model);
            if (obj > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "添加成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "添加失败";
            }
            return Json(result);
        }
        #endregion

        #region 修改
        public ActionResult Edit(int id)
        {
            var model = merchantOpentimeService.Get(x => x.id == id);
            if (model == null)
            {
                return Content("参数错误");
            }
            ViewBag.Opentime = OpenTimeList();
            return View(model);
        }

        [HttpPost]
        public JsonResult Edit(mp_merchant_opentime model)
        {
            var result = new BaseResult();
            if (model == null)
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "参数错误";
                return Json(result);
            }
            var sTime = Convert.ToDateTime(DateTime.Now.Date.ToString("yyyy-MM-dd") + " " + model.merchantopenbegin);
            var eTime = Convert.ToDateTime(DateTime.Now.Date.ToString("yyyy-MM-dd") + " " + model.merchantopenend);
            if (sTime >= eTime)
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "结束时间必须大于开始时间";
                return Json(result);
            }

            var b = merchantOpentimeService.UpdateIgnore(model,
                x => new { x.is_delete },
                x => new object[] { x.id });
            if (b > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "修改成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "修改失败";
            }
            return Json(result);

        }
        #endregion

        private IList<KeyValuePair<string, string>> OpenTimeList()
        {
            return new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>("1","1点"),
                new KeyValuePair<string, string>("2","2点"),
                new KeyValuePair<string, string>("3","3点"),
                new KeyValuePair<string, string>("4","4点"),
                new KeyValuePair<string, string>("5","5点"),
                new KeyValuePair<string, string>("6","6点"),
                new KeyValuePair<string, string>("7","7点"),
                new KeyValuePair<string, string>("8","8点"),
                new KeyValuePair<string, string>("9","9点"),
                new KeyValuePair<string, string>("10","10点"),
                new KeyValuePair<string, string>("11","11点"),
                new KeyValuePair<string, string>("12","12点"),
                new KeyValuePair<string, string>("13","13点"),
                new KeyValuePair<string, string>("14","14点"),
                new KeyValuePair<string, string>("15","15点"),
                new KeyValuePair<string, string>("16","16点"),
                new KeyValuePair<string, string>("17","17点"),
                new KeyValuePair<string, string>("18","18点"),
                new KeyValuePair<string, string>("19","19点"),
                new KeyValuePair<string, string>("20","20点"),
                new KeyValuePair<string, string>("21","21点"),
                new KeyValuePair<string, string>("22","22点"),
                new KeyValuePair<string, string>("23","23点"),
                new KeyValuePair<string, string>("24","24点")
            };
        }
    }
}